package com.briup.filedemo.web;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 文件上传
 */
@RestController
public class FileController {
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @PostMapping("/upload")
    public String upload(@RequestParam(value = "file",required = false) MultipartFile mf) throws Exception {
       //判断文件是否为空,默认已使用@RequestParam限制
        if(mf.isEmpty()){
            return "上传文件为空";
        }
        //获取文件名
        String fileName = mf.getOriginalFilename();
        System.out.println("文件名："+fileName);
        //获取文件字节信息
        byte[] bytes = mf.getBytes();
        System.out.println("文件字节大小"+bytes.length);
        //获取文件输入流信息
        InputStream in = mf.getInputStream();
        //获取文件
        long size = mf.getSize();
        System.out.println("文件字节大小："+size);
        //保存文件
        String path = "D:/"+fileName;
        mf.transferTo(new File(path));
        return "file upload success";
    }
}
